/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package entities;

import java.io.Serializable;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import java.util.ArrayList;
import java.util.List;
import javax.persistence.CascadeType;
import javax.persistence.FetchType;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.xml.bind.annotation.XmlRootElement;

/**
 *
 * @author Nordinator
 */
@Entity
@Table(name="ALBUM")
@XmlRootElement
@NamedQueries({
    @NamedQuery(name = "Album.findAll", query = "SELECT a FROM Album a"),
    @NamedQuery(name = "Album.findByName", query = "SELECT a FROM Album a WHERE a.name = :name")})
public class Album implements Serializable {
    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;
    private String name;
    //Cette ArrayListe contiendra les noms des albums
    @OneToMany(cascade=CascadeType.ALL, fetch=FetchType.EAGER)
    private List<Chanson> albumSongs;

    /**
     * Default constructor
     */
    public Album() {
    }
    
    /**
     * Constructor 
     * @param : name = nom de l'album
     */
    public Album(String name){
        this.name = name;
        albumSongs = new ArrayList();
    }
    
    /**
     * Get the value of albumSongs
     *
     * @return the value of albumSongs
     */
    public List<Chanson> getAlbumSongs() {
        return albumSongs;
    }
    
    /**
     * Verifie si une chanson est contenue 
     */
    public boolean containSong(Chanson song){
        for(Chanson tmp : albumSongs){
            if(tmp.getTitre().equals(song.getTitre())){
                return true;
            }
        }
        return false;
    }

    /**
     * Set the value of albumSongs
     *
     * @param albumSongs new value of albumSongs
     */
    public void setAlbumSongs(ArrayList<Chanson> albumSongs) {
        this.albumSongs = albumSongs;
    }

    /**
     * 
     * @return 
     */
    public Long getId() {
        return id;
    }
    
    /**
     * 
     * @param id 
     */
    public void setId(Long id) {
        this.id = id;
    }
    
    /**
     * 
     * @return 
     */
    public String getName(){
        return name;
    }
    
    /**
     * 
     * @param name 
     */
    public void setName(String name){
        this.name = name;
    }
    
    /**
     * Methode d'ajout d'une chanson ds un album
     * @param chanson 
     */
    public void addSong(Chanson chanson){
        albumSongs.add(chanson);
    }

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (id != null ? id.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof Album)) {
            return false;
        }
        Album other = (Album) object;
        if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return name;
    }
    
}
